package com.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.*;
import com.exam.service.EvaluationService;
import com.exam.serviceimpl.FillQuestionServiceImpl;
import com.exam.serviceimpl.JudgeQuestionServiceImpl;
import com.exam.serviceimpl.MultiQuestionServiceImpl;
import com.exam.serviceimpl.PaperServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Company：
 * @Author:
 * @Date: 2022/5/1 17:05
 * @Version 1.0
 */
@RestController
public class EvaluationController {
    @Resource
    private EvaluationService evaluationService;

    @Autowired
    private PaperServiceImpl paperService;

    @Autowired
    private JudgeQuestionServiceImpl judgeQuestionService;

    @Autowired
    private MultiQuestionServiceImpl multiQuestionService;

    @Autowired
    private FillQuestionServiceImpl fillQuestionService;

    @PostMapping("/addEvaluation")
    public ApiResult add(@RequestBody Evaluation evaluation, HttpSession session) {
        evaluation.setCreateDate(new Date());
        evaluationService.add(evaluation);
        return ApiResultHandler.buildApiResult(200, "成功'", evaluation.getId());
    }

    @PostMapping("/addResult")
    public ApiResult addResult(@RequestBody TestResult testResult) {
        // 查询是否已提交答案
        Integer count = evaluationService.get(testResult);
        if (count != null && count > 0) {
            return ApiResultHandler.success(evaluationService.updateResult(testResult));
        } else {
            return ApiResultHandler.success(evaluationService.addResult(testResult));
        }
    }

    /**
     * @return
     * @Date 2022/5/2 11:48
     * @Author
     * @Description //TODO 查询测试
     **/
    @GetMapping("/evaluation/{id}")
    public Map<String, List<?>> findById(@PathVariable("id") Integer id) {
        Evaluation evaluation = evaluationService.findById(id);
        Integer paperId = evaluation.getPaperId();
        // 获取试题内容
        List<MultiQuestion> multiQuestionRes = multiQuestionService.findByIdAndType(paperId);   //选择题题库 1
        List<FillQuestion> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId);     //填空题题库 2
        List<JudgeQuestion> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);
// 查询答案
        List<TestResult> multiTestResults = evaluationService.findAnswer(evaluation.getId(), paperId, 1);
        List<TestResult> fillTestResults = evaluationService.findAnswer(evaluation.getId(), paperId, 2);
        List<TestResult> judgeTestResults = evaluationService.findAnswer(evaluation.getId(), paperId, 3);
        for (MultiQuestion multiQuestionRe : multiQuestionRes) {
            for (TestResult multiTestResult : multiTestResults) {
                if(multiQuestionRe.getQuestionId().equals(multiTestResult.getQuestionId())){
                    multiQuestionRe.setRightAnswer(multiTestResult.getAnswer());
                }
            }
        }
        for (FillQuestion fillQuestionsRe : fillQuestionsRes) {
            for (TestResult fillTestResult : fillTestResults) {
                if(fillQuestionsRe.getQuestionId().equals(fillTestResult.getQuestionId())){
                    fillQuestionsRe.setAnswer(fillTestResult.getAnswer());
                }
            }
        }
        for (JudgeQuestion judgeQuestionRe : judgeQuestionRes) {
            for (TestResult judgeTestResult : judgeTestResults) {
                if(judgeQuestionRe.getQuestionId().equals(judgeTestResult.getQuestionId())){
                    judgeQuestionRe.setAnswer(judgeTestResult.getAnswer());
                }
            }
        }
        Map<String, List<?>> map = new HashMap<>();
        map.put("multi",multiQuestionRes);
        map.put("fill",fillQuestionsRes);
        map.put("judge",judgeQuestionRes);
        return map;
    }


    @GetMapping("/findEvaluation")
    public ApiResult findAll( Integer page,  Integer size,Integer userId,String teacherName,String source){
        Page<Evaluation> teacherPage = new Page<>(page,size);
        IPage<Evaluation> teacherIPage = evaluationService.findAll(teacherPage,userId,teacherName,source);
        return ApiResultHandler.buildApiResult(200,"查询所有测试记录",teacherIPage);
    }
}


